package com.cql.sys.service.impl;

import com.cql.pojo.Role;
import com.cql.sys.mapper.RoleMapper;
import com.cql.sys.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Override
    public List<Role> roles() throws Exception {
        return roleMapper.roles();
    }

    @Override
    public int insert(Role role) throws Exception {

        Role role1 = roleMapper.findByName(role.getRoleName());

        return null == role1 ? roleMapper.add(role) : -1; // 判断角色名是否已存在, 不存在就添加, 存在则返回-1
    }

    @Override
    public Role findByName(String name) throws Exception {
        return roleMapper.findByName( name);
    }

    @Override
    public int update(Role role) throws Exception {
        Role role1 = roleMapper.findByName(role.getRoleName());

        System.out.println("@"+roleMapper.update(role) );
        return null ==  role1 ? roleMapper.update(role) : -1;
    }

    @Override
    public int delete(Role role) throws Exception {
        Role role1 = roleMapper.findByName(role.getRoleName());

        return null == role1 ? roleMapper.delete(role) : -1;
    }
}
